package com.app.Controller;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;

import com.app.Activity.HelloForm1;
import com.app.Activity.R;
import com.app.Activity.frmBieuQuyetMoiRoiNhom;
import com.app.Activity.frmChatPeerActivity;
import com.app.Activity.frmMatKetNoiActivity;
import com.app.Activity.frmNhomChoActivity;
import com.app.Activity.frmNotificationMoiVaoNhom;
import com.app.Activity.frmPhuActivity;
import com.app.Activity.tabActivity;
import com.app.Activity.trucActivity;

public class ActivityController {
	private static ArrayList<ArrayList<String>> arraylist;
	private static int _countdg = 0;
	private static int _valueAgree = 0;
	private static int _valueDisAgree = 0;
	private static int _count = 0;

	private static int _index = 0;
	private static Activity peerActivity = null;
	// public static String mess;
	public static Handler hander = new Handler();

	public static void showGroupInvite() {
		hander.post(new Runnable() {

			@Override
			public void run() {
				frmPhuActivity.show("Co nguoi moi ban vao nhom");

			}
		});
	}

	public static void showError() {
		frmPhuActivity.show("Bạn ko có quyền này");
	}

	public static void showMoiVaoNhomNotification(String groupName, String msg) {
		frmPhuActivity.contentText = groupName;
		frmPhuActivity.contentTitle = msg;
		hander.post(frmPhuActivity.raMoiVaoNhomNotif);
	}

	public static void setPeerActivity(Activity peerActivity) {
		ActivityController.peerActivity = peerActivity;
	}

	public static void showNotification(String title, String contentTitle, String contentText) {
		Intent intent = new Intent(peerActivity, frmNotificationMoiVaoNhom.class);
		intent.putExtra("ID", 1);
		PendingIntent pendingIntent = PendingIntent.getActivity(peerActivity, 0, intent, 0);
		NotificationManager notiManager = (NotificationManager) peerActivity.getSystemService(peerActivity
				.getBaseContext().NOTIFICATION_SERVICE);
		Notification notif = new Notification(R.drawable.ic_launcher, title, System.currentTimeMillis());
		notif.setLatestEventInfo(peerActivity, contentTitle, contentText, pendingIntent);

		notiManager.notify(1, notif);

	}

	public static void showNhomChoActivity() {
		// Intent i=new Intent()
	}

	public static void startGroup() {
		hander.post(frmNhomChoActivity.raStartGroup);
	}



	public static void showMatKetNoi() {
//		Intent intent = new Intent(MESApplication.getAppContext(), frmMatKetNoiActivity.class);
//		if (!frmMatKetNoiActivity.isShowed) {
//			MESApplication.getAppContext().startActivity(intent);
//		}

	}
	public static void closeMatKetNoi(){
		hander.post(frmMatKetNoiActivity.raClose);
	}
	// =============================================truc==================================

	public static void ShowMessageTAB(final String message) {
		tabActivity.message = message;
		hander.post(tabActivity.showMessage);
	}

	public static void ShowMessage(final String message) {
		trucActivity.message = message;
		hander.post(trucActivity.showMessage);
	}

	// hien thi noi dung chat

	public static void ShowChatMessage(final String message, final String address) {
		trucActivity.chatMessage = message;
		trucActivity.addressFriend = address;
		hander.post(trucActivity.showChatMessage);
	}

	public static void DislayKickMember() {
		hander.post(tabActivity.dislayKickMember);

	}

	public static void ShowFrm2() {
		hander.post(HelloForm1.showFrm2);
	}

	public static void ShowFrm1() {
		hander.post(trucActivity.showForm1);
	}

	public static void ShowResultVote() {
		hander.post(trucActivity.showResultVoteKick);
	}

	public static void FinishDiscussActivity() {
		hander.post(trucActivity.finishAct);
	}

	public static void DoEndTime() {
		hander.post(frmBieuQuyetMoiRoiNhom.doEndTime);
	}

	// public static void HienThiTinNhanChatRieng(){
	// hander.post(frmChatPeerActivity.showMessageChat);
	// }

	// chat rieng

	public static void updateMessageChatPeer(final String ipReceiver, final String userName) {
		frmChatPeerActivity._IPFriend = ipReceiver;
		frmChatPeerActivity._userNameFriend = userName;
		frmChatPeerActivity._FriendChatting = userName;
		// frmChatPeerActivity.updateMessage();=====================================================
	}

	public static void ShowChatPeer(final String ipReceiver, final String userName) {
		trucActivity.ipSender = ipReceiver;
		trucActivity.userNameSender = userName;
		hander.post(trucActivity.showFormChatPeer);
	}

	public static void FinishChatPeerActivity() {
		hander.post(frmChatPeerActivity.closeMyForm);
	}

	public static void ShowChatPeerNonParamater() {

		hander.post(trucActivity.showFormChatPeer);
	}

	public static Activity getPeerActivity() {
		return peerActivity;
	}

	public static void ShowChatConversation() {
		hander.post(trucActivity.showFormConversation);
	}

	// end chat rieng
	// ===============================end truc=================================

	public static void ShowAlert(Context context, String title, String message, int icon) {
		AlertDialog.Builder buider = new Builder(context);
		buider.setTitle(title);
		buider.setMessage(message);
		buider.setIcon(icon);
		buider.setNegativeButton("OK", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

			}
		});
		buider.show();
	}
	
	//**********
	// ====An
	//**********
	public static void show_Error() {
		trucActivity.show("Ban khong co quyen nay");
	}

	public static void ShowSignOut(String name) {
		Log.d("PEERDROID", "vao show sign out: " + name);
		// hander.post(trucActivity.raSignout);
		trucActivity.showSignOut(name);
	}

	public static void ShowDanhGia(Address sender) {
		Log.d("PEERDROID", "vao show danh gia");
		//hander.post(trucActivity.raShowDanhGia);
		trucActivity.showRequestDanhGia(sender);
	}

	// Ham Tinh ket qua Danh Gia
	public static void CountResultDanhGia(String result) {
		Log.d("PEERDROID", "Ket qua da nhan duoc: " + result);
		String[] s = result.split(";");
		String[] d = null;
		ArrayList<String> array;
		_countdg++;
		if (arraylist == null) {
			Log.d("PEERDROID", "arraylist null ");
			arraylist = new ArrayList<ArrayList<String>>();
			for (int i = 0; i < s.length; i++) {

				array = new ArrayList<String>();
				Log.d("PEERDROID", "s: " + s[i].toString());
				d = s[i].split("/");
				int j = 0;
				while (j < 2) {

					array.add(d[j].toString());
					j++;
				}
				arraylist.add(array);

				Log.d("PEERDROID", "arraylist get : " + arraylist.get(i));
			}
		} else {
			Log.d("PEERDROID", "arraylist not null ");
			for (int i = 0; i < s.length; i++) {

				array = new ArrayList<String>();
				Log.d("PEERDROID", "s: " + s[i].toString());
				d = s[i].split("/");
				int j = 0;
				while (j < 2) {

					array.add(d[j].toString());
					j++;
				}
				for (int k = 0; k < arraylist.size(); k++) {
					Log.d("PEERDROID", "i =  " + String.valueOf(k));
					Log.d("PEERDROID", "vao vong lap For arraylist " + arraylist.get(k).get(0));
					Log.d("PEERDROID", "array.get(0) " + array.get(0));

					if (arraylist.get(k).get(0).equals(array.get(0))) {

						Log.d("PEERDROID", "vao vong lap For arraylist ->> if ");

						float point = Float.parseFloat(arraylist.get(k).get(1));
						Log.d("PEERDROID", "point : " + String.valueOf(point));

						float pointnew = Float.parseFloat(array.get(1));
						Log.d("PEERDROID", "point new: " + String.valueOf(pointnew));

						point += pointnew;
						Log.d("PEERDROID", "point updated: " + String.valueOf(point));

						String p = String.valueOf(point);
						Log.d("PEERDROID", " arraylist get k " + arraylist.get(k));

						arraylist.get(k).add(1, p);
						Log.d("PEERDROID", " arraylist get last " + arraylist.get(k));
					} else {
						Log.d("PEERDROID", "sai bet: ");
					}
				}
			}
		}
	}

	// Ham tinh ket qua Bieu Quyet
	public static void CountResultBieuQuyet(int index, int value) {
		Log.d("PEERDROID", "Ket qua bieu quyet nhan duoc: " + String.valueOf(index) + "-" + String.valueOf(value));
		_count++;
		_index = index;
		if (value == 0) {
			if (_valueDisAgree == 0) {
				_valueDisAgree ++;
			} else {
				_valueDisAgree += 1;
			}
		} else {
			if (value == 1 && _valueAgree == 0) {
				_valueAgree ++;
			} else {
				_valueAgree += 1;
			}
		}
	}
	
	public static void WriteTextToLog(String text,String name){
		Log.d("Phu", "Da vào WriteTextToLog trong ActivityController" );
		Log.d("Phu", NetworkController._peer.getAccount().get_Username() + "-"+ text );
        //String username = NetworkController._peer.getAccount().get_Username();
        String tenNhom = NetworkController.getGroupName();
		Log.d("Phu", "ten nhom 2....: "+ tenNhom);
		String path = ReadWriteFileController.DIRECTORY + tenNhom+"LogList.xml";
		Log.d("Phu", "Path: "+ path);
        ReadWriteFileController.insertLogFile(path, text, name);
	}

	public static int get_countdg() {
		return _countdg;
	}

	public static void set_countdg(int _countdg) {
		ActivityController._countdg = _countdg;
	}

	public static ArrayList<ArrayList<String>> getArraylist() {
		return arraylist;
	}

	public static void setArraylist(ArrayList<ArrayList<String>> arraylist) {
		ActivityController.arraylist = arraylist;
	}

	public static int get_valueAgree() {
		return _valueAgree;
	}

	public static void set_valueAgree(int _valueAgree) {
		ActivityController._valueAgree = _valueAgree;
	}

	public static int get_valueDisAgree() {
		return _valueDisAgree;
	}

	public static void set_valueDisAgree(int _valueDisAgree) {
		ActivityController._valueDisAgree = _valueDisAgree;
	}

	public static int get_count() {
		return _count;
	}

	public static void set_count(int _count) {
		ActivityController._count = _count;
	}

	public static int get_index() {
		return _index;
	}

	public static void set_index(int _index) {
		ActivityController._index = _index;
	}

	// public static void AddNode(final int parentId,final Node node){
	// frmTreeNodeActivity.parentId=parentId;
	// frmTreeNodeActivity.nodeAdd=node;
	// hander.post(frmTreeNodeActivity.raAddnode);
	// }

	public static void ShowMessageKicked() {
		hander.post(tabActivity.dislayKickedMessage);
	}

}
